package com.lxb.myforum.controller.admin.sysmgt

import javax.servlet.http.HttpServletRequest

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.PageRequest
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody

import com.lxb.myforum.domain.PageResponse
import com.lxb.myforum.pojo.Role
import com.lxb.myforum.pojo.User
import com.lxb.myforum.service.UserService

/**
 *
 * @author lxb
 */
@Controller("adminUserCtrl")
@RequestMapping("/admin/users")
class UserController @Autowired
constructor(private val userService: UserService) {

    @GetMapping(value = ["", "/"])
    fun users(): String {
        return "/admin/sysmgt/users/index"
    }

    @GetMapping(value = ["/all"])
    @ResponseBody
    fun getUsers(page: Int, size: Int): PageResponse<User> {
        val pageable = PageRequest.of(page - 1, size)
        val users = userService.getUsers(pageable)
        return PageResponse(
                0L,
                page = users.number.toLong(),
                size = users.size.toLong(),
                rows = users.content,
                totalCount = users.totalElements,
                totalPage = users.totalPages.toLong()
        )
    }

    /**
     * 获取该角色相关的权限信息： 包括拥有什么权限、未拥有的权限
     * @param userId 用户id
     */
    @GetMapping(value = ["/{id}/roles"])
    fun getRoles(req: HttpServletRequest, @PathVariable("id") userId: Long): String {
        val ownRoles = userService.findOwnRoles(userId)
        req.setAttribute("ownRoles", ownRoles)
        req.setAttribute("notOwnRoles", userService.findNotOwnRoles(userId))
        return "/admin/sysmgt/users/userRole"
    }


    /**
     * 获取该角色相关的权限信息： 包括拥有什么权限、未拥有的权限
     *
     * 如果isOwn = true，那么就是该角色拥有的权限
     *
     * 如果isOwn = false，那么就是该角色未拥有的权限
     * @param isOwn 是否为自己的
     */
    @GetMapping(value = ["/{id}/roles_list"])
    @ResponseBody
    fun getUserRoles(req: HttpServletRequest,
                     @PathVariable("id") userId: Long,
                     @RequestParam(value = "isOwn", defaultValue = "1") isOwn: Boolean): PageResponse<Role> {
        return if (isOwn) {
            PageResponse(
                    0L,
                    rows = userService.findOwnRoles(userId)
            )
        } else PageResponse(
                0L,
                "",
                -1,
                rows = userService.findNotOwnRoles(userId)
        )
    }

    @DeleteMapping(value = ["/{id}/roles"])
    @ResponseBody
    fun delRoles(@PathVariable("id") userId: Long,
                 @RequestParam("roleId[]") roleIds: Set<Long>): Map<String, Any> {
        val result = userService.delUserRoles(userId, roleIds)
        if (result["result"] as Int > 0) {
            result["ownRoles"] = userService.findOwnRoles(userId)
            result["notOwnRoles"] = userService.findNotOwnRoles(userId)
        }
        return result
    }

    @PostMapping(value = ["/{id}/addRoles"])
    @ResponseBody
    fun addRoles(@PathVariable("id") userId: Long,
                 @RequestParam("roleId[]") roleIds: Set<Long>): Map<String, Any> {
        val result = userService.addUserRoles(userId, roleIds)
        if (result["result"] as Int > 0) {
            result["ownRoles"] = userService.findOwnRoles(userId)
            result["notOwnRoles"] = userService.findNotOwnRoles(userId)
        }
        return result
    }
}
